// JavaScript Document
function addBook(bookjson){
	return addBookList($('#bookList'),bookjson,true,false);
}
function getinput(name,form){
  	if(!form)
	  	form='addBookForm'
	return $('#'+form+' input[name="'+name+'"]').eq(0);
}
function getlabel(name,form){
  	if(!form)
	  	form='addBookForm'
	return $('#'+form+' label[for="'+name+'"]').eq(0).text();
}
function makeAccordion(){
	makeObjAccordion($('#bookList'));
}
$(function() {
	var allBookFields = $('#addBookForm .text');
	var allListFields = $('#editListForm .text');
	$('#addBookForm').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			"確定新增":function(){
				var title = getinput('title');
				var author = getinput('author');
				var publisher = getinput('publisher');
				var isbn = getinput('isbn');
				var price = getinput('price');
				var thumb = getinput('thumb');
				var link = getinput('link');

				var valid=true;
				allBookFields.removeClass('ui-state-error');
				if(valid){
					$.ajax({
						url: 'panel.php?page=editlist&action=addbook&id='+$('#getid').val(),
						dataType:'json',
						data: {
							"title":title.val(),
							"author":author.val(),
							"publisher":publisher.val(),
							"isbn":isbn.val(),
							"price":price.val(),
							"thumb":thumb.val(),
							"link":link.val()
						},
						type: 'POST',
						beforeSend: function(){
							updateTips('請稍後');
						},
						error: function(){
							updateTips('伺服器發生錯誤，請稍後再試');
						},
						success: function(response){
							console.log(response);
							if(response.status=='success'){
								addBook(response);
								$('#bookList').accordion('destroy');
								makeAccordion();
								$('#addBookForm').dialog("close");
							}else{
								if(response.type=='mysql'){
									updateTips('Mysql發生錯誤，請稍後再試或聯絡管理員');
								}else{
									var t=getlabel(response.field);
									switch(response.type){
										case 'length':
											t += ' 字數必須介於 ' + response.min + ' 和 ' + response.max + '之間';
											break;
										case 'int':
											t += ' 必須是數字';
											if(response.min)
												t += '，大於 '+response.min;
											if(response.max)
												t += '，小於 '+response.max;
											break;
										case 'link':
											t += ' 必須要是網址(只接受http://或https://)';
											break;
										default:
											t += ' 發生錯誤';
									}
									getinput(response.field).focus();
									updateObjTips(getinput(response.field),t);
								}
							}
						}
					});
				}
			},
			"取消":function(){
				$(this).dialog("close");
			}
		},
		close: function(){
			allBookFields.val("").removeClass('ui-state-error');
			resetTips();
		}
	});
	$('#editListForm').dialog({
		autoOpen: false,
		modal: true,
		buttons:{
			"確定修改":function(){
				alert('現在暫不提供修改功能');
			},
			"取消":function(){
				$(this).dialog("close");
			}
		},
		close: function(){
			allListFields.val("").removeClass('ui-state-error');
			resetTips();
		}
	});

	$('#addBook').click(function(){
		$('#addBookForm').dialog('open');
	});
	$('#editList').click(function(){
		$('#editListForm').dialog('open');
	});
	$('#viewFollow').click(function(){
		location.href='panel.php?page=editlist&action=viewfollow&id='+$('#getid').val();
	});
	$('#editList').button("option","disabled",true);
	makeAccordion();
});
